package com.disney.dolcn.invitemail.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 
 * @author hawang
 * 
 */
public class ContextUtil {
	private static String encoding = "GBK";

	/**
	 * read template file
	 * 
	 * @param fileName
	 * @return
	 * @throws LogicException
	 */
	public static String readMailTemplate(String fileName) throws Exception {
		return readMailTemplate(new File(fileName));
	}

	/**
	 * 
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String readMailTemplate(File fileName) throws Exception {
		StringBuffer sb = new StringBuffer();
		InputStream is = null;
		LineNumberReader reader = null;
		try {
			is = new FileInputStream(fileName);
			reader = new LineNumberReader(new InputStreamReader(is, encoding));
			String str = null;
			while ((str = reader.readLine()) != null) {
				sb.append(str);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				reader = null;
			}
			if (is != null) {
				try {
					is.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				is = null;
			}
		}
		return sb.toString();
	}

	/**
	 * prepare new template from old template. regExs和replaceontent相对应.
	 * 
	 * @param oldContent
	 * 
	 * @param regExs
	 *            正则表达式
	 * @param replaceontent
	 *            要替换的内容
	 * @return
	 * @throws LogicException
	 */
	public static String prepareTemplate(String oldContent, String[] regExs,
			String[] replaceontent) {

		String content = oldContent;
		System.out.println("替换前邮件内容长度" + oldContent.length());
		try {
			for (int i = 0; i < regExs.length; i++) {
				System.out.println("替换前邮件内容长度" + regExs[i]);
				content = content.replaceAll(regExs[i], replaceontent[i]);
			}
		} catch (Exception d) {
			d.printStackTrace();
		}
		System.out.println("替换后邮件内容长度" + content.length());
		return content;
	}

	/**
	 * Replace sensitive key words.
	 * 
	 * @param oldContent
	 *            user's comment
	 * @param keySet
	 *            sensitive words
	 * @return String
	 */
	public static String repalceContent(final String oldContent,
			final List<String> keySet) {
		String content = oldContent;
		if (keySet != null) {
			for (int i = 0; i < keySet.size(); i++) {
				String key = keySet.get(i);
				content = content.replace(key, InviteConstant.REPLACESTR);
			}
		}
		return content;
	}

	/**
	 * 
	 * @param oldContent
	 * @param keySet
	 * @return
	 * @return 0 没有包含非法字符 1包含
	 */
	public static int findContentKeys(final String oldContent,
			final List<String> keySet) {
		String content = oldContent;
		if (keySet != null) {
			for (int i = 0; i < keySet.size(); i++) {
				String key = keySet.get(i);
				if (content.indexOf(key) > -1)
					return 1;
			}
		}
		return 0;
	}

	public static void main(String sendmail[]) {

		String OLD_STR[] = { "[#member.name#]", "(name)" };
		String NEW_STR[] = { "wanghao", "(name)" };
		String context_ = "迪士尼中国官网邮件";
		try {
			for (int i = 0; i < OLD_STR.length; i++) {
				context_ = context_.replaceAll(OLD_STR[i], NEW_STR[i]);
			}
			System.out.println(context_);
			// 邮件发送失败
		} catch (Exception d) {
			d.printStackTrace();
		}

	}

}
